package juego.main;


import java.io.BufferedInputStream;
import java.io.InputStream;

import javazoom.jl.player.Player;

/**
 * Clase que permite reproducir archivos mp3.
 * @author Latini Macarena
 * @author Rosas Tifany
 */
public class SoundPlayer {
	private Player player;

	private PlayThread PT;

	private String filename;

	public SoundPlayer(String dir) {
		this.filename = dir;
	}
	/**
	 * Comienza la reproduccion del archivo.
	 */
	public void play() {
		try {
			InputStream fis = getIS(filename);
			BufferedInputStream bis = new BufferedInputStream(fis);
			player = new Player(bis);

			this.PT = new PlayThread();
			PT.start();

		} catch (Exception e) {
		}
	}

	private InputStream getIS(String dir) {
		return getClass().getResourceAsStream(dir);
	}

	public void replay() {
		play();
	}

	public void stop() {

		if (player != null)
			player.close();

	}

	public Player getPlayer() {
		return player;
	}

	private class PlayThread extends Thread {

		public void run() {
			try {
				player.play();

				while (true)
					if (player.isComplete()) {
						break;
					}

			} catch (Exception e) {
			}
		}
	}

}
